package com.qingge.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qingge.springboot.entity.User;
import com.qingge.springboot.mapper.UserMapper;
import com.qingge.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/user")
public class UserController {
//    @Autowired  引入方法
//    private UserMapper userMapper;

    @Autowired
    private UserService userService;

    // 新增和修改
    @PostMapping
    //@RequestBody 把前台json对象转化为java对象
    public boolean save(@RequestBody User user){
        // 新增或者更新
        return userService.saveUser(user);
    }
    @GetMapping
    public List<User> findAll(){
        return userService.list();
    }

    @DeleteMapping("/{id}") //这个id和下面的id一一对应
    public boolean delete(@PathVariable Integer id){
        return userService.removeById(id);
    }

    //批量删除接口
    @PostMapping("/del/batch") //这个id和下面的id一一对应
    public boolean deleteBatch(@RequestBody List<Integer> ids){
        return userService.removeByIds(ids); //请求参数是变量，用RequestBody注解
    }

    //分页查询
    //接口路径 /user/page?pageNum=1&pageSize=10
    // @Request接收
    // limit第一个参数 = (pageNum - 1) * pageSize
    //pageSize
//    @GetMapping("/page")
//    public Map<String, Object> findPage(@RequestParam Integer pageNum,
//                                        @RequestParam Integer pageSize,
//                                        @RequestParam String username){
//        pageNum = (pageNum - 1) * pageSize;
//        // 模糊查询也可以这样写 username = '%' + username + '%';
//        List<User> data = userMapper.selectPage(pageNum,pageSize,username);
//        //List<User> data = userMapper.selectPage(pageNum,pageSize,username,email,address);
//        Integer total = userMapper.selectTotal(username);
//        //Integer total = userMapper.selectTotal(username,email,address);
//        Map<String,Object> res = new HashMap<>();
//        res.put("data",data);
//        res.put("total",total);
//        return res;
//    }

    // 分页查询 - mybatis-plus
    @GetMapping("/page")
    public IPage<User> findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam(defaultValue = "") String username,
                                @RequestParam(defaultValue = "") String email,
                                @RequestParam(defaultValue = "") String address){
        IPage<User> page = new Page<>(pageNum,pageSize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if(!"".equals(username)){
            queryWrapper.like("username",username);
        }
        if(!"".equals(email)){
            queryWrapper.like("email",email);
        }
        if(!"".equals(address)){
            queryWrapper.like("address",address);
        }
        queryWrapper.orderByDesc("id");
        return userService.page(page,queryWrapper);
    }
}
